package com.red.wood.module.eval.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.eval.mybatis.service.EvalPassEnterpriseNewService;
import com.red.wood.module.eval.mybatis.service.EvalPassEnterpriseService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/evalPassEnterpriseNew")
public class EvalPassEnterpriseNewController {
	
	private static final Log log = LogFactory.getLog(EvalPassEnterpriseNewController.class);
	
	@Autowired
	private EvalPassEnterpriseNewService service;

	/**
	 * 评测通过企业
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView("evalPassEnterpriseNew");
		mv.addObject("userId",CommonUtils.valueOf(request.getParameter("userId")));
		return mv;
	}
	
	/**
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "getEvalPassEnterpriseInfo", method = RequestMethod.POST)
	@ResponseBody
	public String getEvalPassEnterpriseInfo(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			IMap param = new IMap(request); 
			int pageSize = Integer.parseInt(CommonUtils.valueOf(request.getParameter("pageSize")));
			String currentPage = CommonUtils.valueOf(request.getParameter("currentPage"));
			Page page = Page.newBuilder(Integer.parseInt(currentPage),pageSize,null);  
			param.put("page",page);
			
			Map<String,Object> rs = service.getPageData(param);
			page.setTotalRecord(Integer.parseInt(CommonUtils.valueOf(rs.get("totalCount"))));
			List<Map<String,Object>> list = (List<Map<String, Object>>) rs.get("pageData");
			
			json.put("flag", true);
			json.put("datas",list);
			json.put("totalPages",page.getTotalPage());
			json.put("totalData", page.getTotalRecord());
			json.put("currentPage", currentPage);
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
			json.put("flag",false);
		}
		return json.toString();
	}
}
